package com.smart.primary.sys.service.impl;

import com.smart.primary.common.util.AjaxResult;
import com.smart.primary.common.util.DateUtil;
import com.smart.primary.common.util.Util;
import com.smart.primary.sys.dao.ResMapper;
import com.smart.primary.sys.dao.RoleMapper;
import com.smart.primary.sys.dao.UserMapper;
import com.smart.primary.sys.service.RoleService;
import com.smart.primary.sys.vo.*;
import com.smart.primary.util.SessionUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper sysRoleMapper;
    @Resource
    private UserMapper sysUserMapper;
    @Resource
    private ResMapper sysResMapper;

    public RoleServiceImpl() {
    }

    public Role queryObject(Role sysRole) throws Exception {
        return (Role)this.sysRoleMapper.queryObject(sysRole);
    }

    public List<Role> queryList(Role sysRole) throws Exception {
        return this.sysRoleMapper.queryList(sysRole);
    }

    public List<Role> queryListByPage(Role sysRole) throws Exception {
        return this.sysRoleMapper.queryListByPage(sysRole);
    }

    public Integer queryCount(Role sysRole) throws Exception {
        Integer ct = this.sysRoleMapper.queryCount(sysRole);
        return ct == null ? 0 : ct;
    }

    public AjaxResult saveOrUpdate(Role sysRole, HttpServletRequest request, HttpServletResponse response) throws Exception {
        int count = this.sysRoleMapper.queryCount(sysRole);
        if (count > 0) {
            return AjaxResult.failure("角色名称已经存在!");
        } else {
            String roleId = sysRole.getRoleId();
            String userId = SessionUtil.getUserId(request,response);
            Date nowTime = DateUtil.changeStrToDate(DateUtil.getNowTime());
            int i;
            if (roleId != null && !roleId.equals("")) {
                sysRole.setUpdateUserId(userId);
                sysRole.setUpdateTime(nowTime);
                i = this.sysRoleMapper.update(sysRole);
                return i > 0 ? AjaxResult.updateSuccess() : AjaxResult.failure("信息修改失败!");
            } else {
                roleId = Util.getUUID();
                sysRole.setRoleId(roleId);
                sysRole.setCreateUserId(userId);
                sysRole.setCreateTime(nowTime);
                sysRole.setUpdateUserId(userId);
                sysRole.setUpdateTime(nowTime);
                sysRole.setRoleState(1);
                i = this.sysRoleMapper.insert(sysRole);
                return i > 0 ? AjaxResult.saveSuccess() : AjaxResult.failure("信息添加失败!");
            }
        }
    }

    public Integer insert(Role sysRole) throws Exception {
        return this.sysRoleMapper.insert(sysRole);
    }

    public Integer update(Role sysRole) throws Exception {
        return this.sysRoleMapper.update(sysRole);
    }

    public Integer delete(String roleIds) throws Exception {
        if (roleIds != null && !roleIds.equals("")) {
            String[] roleIdArr = roleIds.split(",");
            String[] var3 = roleIdArr;
            int var4 = roleIdArr.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String roleId = var3[var5];
                Role sysRole = new Role();
                sysRole.setRoleId(roleId);
                Role tempBean = (Role)this.sysRoleMapper.queryObject(sysRole);
                this.sysRoleMapper.delete(roleId);
                int isSystem = tempBean.getIsSystem();
                if (0 == isSystem) {
                    RoleRes sysRoleRes = new RoleRes();
                    sysRoleRes.setRoleId(roleId);
                    this.sysRoleMapper.deletRoleRes(sysRoleRes);
                    UserRole sysUserRole = new UserRole();
                    sysUserRole.setRoleId(roleId);
                    this.sysUserMapper.deletUserRole(sysUserRole);
                }
            }
        }

        return 1;
    }

    public int authorizationToRole(String roleId, String resIds) {
        if (roleId != null && !roleId.equals("")) {
            RoleRes bean = new RoleRes();
            bean.setRoleId(roleId);
            this.sysRoleMapper.deletRoleRes(bean);
            if (resIds != null && !resIds.equals("")) {
                String[] resIdArr = resIds.split(",");
                String[] var5 = resIdArr;
                int var6 = resIdArr.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    String resId = var5[var7];
                    RoleRes sysRoleRes = new RoleRes();
                    sysRoleRes.setId(Util.getUUID());
                    sysRoleRes.setRoleId(roleId);
                    sysRoleRes.setResId(resId);
                    this.sysRoleMapper.insertRoleRes(sysRoleRes);
                }
            }
        }

        return 1;
    }

    public List<RoleRes> getRoleResList(RoleRes sysRoleRes) {
        List<RoleRes> list = this.sysRoleMapper.getRoleResList(sysRoleRes);
        return list;
    }

    public List<Object> getResListTree(Res sysRes, String roleId) throws Exception {
        RoleRes sysRoleRes = new RoleRes();
        sysRoleRes.setRoleId(roleId);
        List<RoleRes> roleResList = this.sysRoleMapper.getRoleResList(sysRoleRes);
        List<String> resIdList = this.getResIdList(roleResList);
        sysRes.setPageSize(9999);
        List<Res> list = this.sysResMapper.queryListByPage(sysRes);
        List<Object> resList = new ArrayList();
        if (list != null && list.size() > 0) {
            Iterator var8 = list.iterator();

            while(var8.hasNext()) {
                Res bean = (Res)var8.next();
                String id = bean.getResId();
                String pid = bean.getParentId();
                String name = bean.getResName();
                TreeNode node = new TreeNode();
                node.setOpen(true);
                node.setId(id);
                node.setParentId(pid);
                node.setName(name);
                node.setNocheck(false);
                if (resIdList.contains(id)) {
                    node.setChecked(true);
                }

                Map<String, String> attrs = new HashMap();
                if ("0".equals(pid)) {
                    attrs.put("rootFlag", "1");
                } else {
                    attrs.put("rootFlag", "0");
                }

                node.setAttrs(attrs);
                node.setParent(false);
                resList.add(node);
            }
        }

        return resList;
    }

    private List<String> getResIdList(List<RoleRes> roleResList) {
        List<String> list = new ArrayList();
        if (roleResList != null && roleResList.size() > 0) {
            Iterator var3 = roleResList.iterator();

            while(var3.hasNext()) {
                RoleRes bean = (RoleRes)var3.next();
                String resId = bean.getResId();
                list.add(resId);
            }
        }

        return list;
    }

}